﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_MAS_FundTypes : IDAL_FIM_MAS_FundTypes
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        //

        public IList<M_FIM_MAS_FundTypes> GetFull()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_FundTypes> infoList = new List<M_FIM_MAS_FundTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT fund_type_seq, fund_type_name, coalesce(fund_per, 0) as fund_per"
                    + ", coalesce(fund_plus_per_max, 0) as fund_plus_per_max"
                    + ", coalesce(fund_min, 0) as fund_min, coalesce(fund_max, 0) as fund_max"
                    + ", coalesce(round_decimal, 0) as round_decimal"
                    + ", case round_decimal"
                        + " when '0' then 'ไม่ปัดเศษทศนิยม'"
                        + " when '1' then 'ปัดเศษทศนิยมทิ้ง (เช่น 437.95 เป็น 437)'"
                        + " when '2' then 'ปัดเศษทศนิยมขึ้น (เช่น 437.05 เป็น 438)'"
                        + " when '3' then 'ปัดเศษทศนิยมตามปกติ (เช่น 437.50 เป็น 438 หรือ 437.49 เป็น 437)'"
                        + " end as round_decimal_name"
                    + ", record_status, created_by, created_when, updated_by, updated_when"
                    + " FROM fim_mas_fund_types"
                    + " WHERE record_status not like @delete_status";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_FundTypes info = new M_FIM_MAS_FundTypes();
                        info.fund_type_seq = Convert.ToInt32(drc[i]["fund_type_seq"]);
                        info.fund_type_name = drc[i]["fund_type_name"].ToString();
                        info.fund_per = cn.ConvertNullDouble(drc[i]["fund_per"]);
                        info.fund_plus_per_max = cn.ConvertNullDouble(drc[i]["fund_plus_per_max"]);
                        info.fund_min = cn.ConvertNullDouble(drc[i]["fund_min"]);
                        info.fund_max = cn.ConvertNullDouble(drc[i]["fund_max"]);
                        info.round_decimal = drc[i]["round_decimal"].ToString();
                        info.round_decimal_name = drc[i]["round_decimal_name"].ToString();

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_FundTypes> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_FundTypes> infoList = new List<M_FIM_MAS_FundTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT fund_type_seq, fund_type_name"
                    + " FROM fim_mas_fund_types"
                    + " WHERE record_status not like @delete_status";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_FundTypes info = new M_FIM_MAS_FundTypes();
                        info.fund_type_seq = Convert.ToInt32(drc[i]["fund_type_seq"]);
                        info.fund_type_name = drc[i]["fund_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_MAS_FundTypes GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_FundTypes info = new M_FIM_MAS_FundTypes();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT fund_type_seq, fund_type_name, coalesce(fund_per, 0) as fund_per"
                    + ", coalesce(fund_plus_per_max, 0) as fund_plus_per_max"
                    + ", coalesce(fund_min, 0) as fund_min, coalesce(fund_max, 0) as fund_max"
                    + ", coalesce(round_decimal, 0) as round_decimal"
                    + ", case round_decimal"
                        + " when '0' then 'ไม่ปัดเศษทศนิยม'"
                        + " when '1' then 'ปัดเศษทศนิยมทิ้ง (เช่น 437.95 เป็น 437)'"
                        + " when '2' then 'ปัดเศษทศนิยมขึ้น (เช่น 437.05 เป็น 438)'"
                        + " when '3' then 'ปัดเศษทศนิยมตามปกติ (เช่น 437.50 เป็น 438 หรือ 437.49 เป็น 437)'"
                        + " end as round_decimal_name"
                    + ", record_status, created_by, created_when, updated_by, updated_when"
                    + " FROM fim_mas_fund_types"
                    + " WHERE fund_type_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.fund_type_seq = Convert.ToInt32(drc[0]["fund_type_seq"]);
                    info.fund_type_name = drc[0]["fund_type_name"].ToString();
                    info.fund_per = cn.ConvertNullDouble(drc[0]["fund_per"]);
                    info.fund_plus_per_max = cn.ConvertNullDouble(drc[0]["fund_plus_per_max"]);
                    info.fund_min = cn.ConvertNullDouble(drc[0]["fund_min"]);
                    info.fund_max = cn.ConvertNullDouble(drc[0]["fund_max"]);
                    info.round_decimal = drc[0]["round_decimal"].ToString();
                    info.round_decimal_name = drc[0]["round_decimal_name"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Update(M_FIM_MAS_FundTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@fund_type_seq", info.fund_type_seq);
                //dbManager.AddParameters(1, "@fund_type_name", info.fund_type_name);
                dbManager.AddParameters(1, "@fund_per", info.fund_per);
                dbManager.AddParameters(2, "@fund_plus_per_max", info.fund_plus_per_max);
                dbManager.AddParameters(3, "@fund_min", info.fund_min);
                dbManager.AddParameters(4, "@fund_max", info.fund_max);
                dbManager.AddParameters(5, "@round_decimal", info.round_decimal);

                dbManager.AddParameters(6, "@updated_by", info.updated_by);
                dbManager.AddParameters(7, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_fund_types"
                    + " SET fund_per = @fund_per"//fund_type_name = @fund_type_name"
                        + ", fund_plus_per_max = @fund_plus_per_max"
                        + ", fund_min = @fund_min"
                        + ", fund_max = @fund_max"
                        + ", round_decimal = @round_decimal"

                        + ", updated_by = @updated_by"
                        + ", updated_when = @updated_when"
                    + " WHERE fund_type_seq = @fund_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
